package com.salt.core.qcc;

import com.alibaba.fastjson.JSONObject;
import com.salt.common.utils.HttpHelper;
import com.salt.core.qcc.props.QccProperties;
import com.salt.core.qcc.utils.QccUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author salt
 * @Date 2022/9/13
 * @Version 1.0.1
 */
@Slf4j
@AllArgsConstructor
public class QccTemplate {

    private final QccProperties qccProperties;

    public String search() {
        String url = qccProperties.getUrl()
                .concat("/ECIV4/Search?key=")
                .concat(qccProperties.getKey());
        JSONObject jsonObject = httpGet(url, this.headMap());
        return null;
    }

    public JSONObject getInfoObject(String searchKey) {
        String url = qccProperties.getUrl()
                .concat("/ECIComplement/GetInfo?key=")
                .concat(qccProperties.getKey())
                .concat("&searchKey=")
                .concat(searchKey);
        return httpGet(url, this.headMap());
    }

    public JSONObject getInfoObject(String searchKey, Map<String, String> headMap) {
        String url = qccProperties.getUrl()
                .concat("/ECIComplement/GetInfo?key=")
                .concat(qccProperties.getKey())
                .concat("&searchKey=")
                .concat(searchKey);
        return httpGet(url, headMap);
    }

    public String getStatus(JSONObject jsonObject) {
        return QccUtil.get(jsonObject, "Status");
    }

    public Map<String, String> getInfoMap(JSONObject jsonObject) {
        JSONObject resultObject = QccUtil.getJsonObject(jsonObject, "Result");
        Map<String, String> infoMap = new HashMap<>();

        infoMap.put("name", QccUtil.get(resultObject, "Name"));
        infoMap.put("creditCode", QccUtil.get(resultObject, "CreditCode"));
        infoMap.put("status", QccUtil.get(resultObject, "Status"));

        JSONObject areaJsonObject = QccUtil.getJsonObject(resultObject, "Area");
        infoMap.put("province", QccUtil.get(areaJsonObject, "Province"));
        infoMap.put("city", QccUtil.get(areaJsonObject, "City"));
        infoMap.put("county", QccUtil.get(areaJsonObject, "County"));

        return infoMap;
    }

    public String getResultInfo(JSONObject jsonObject) {
        JSONObject resultObject = QccUtil.getJsonObject(jsonObject, "Result");
        return resultObject.toJSONString();
    }

    private Map<String, String> headMap() {
        String timeSpan = QccUtil.timeSpan();
        String token = QccUtil.qccToken(qccProperties.getKey(), timeSpan, qccProperties.getSecret());
        Map<String, String> headMap = new HashMap<>();
        headMap.put("token", token);
        headMap.put("timeSpan", timeSpan);
        return headMap;
    }


    @SneakyThrows
    private JSONObject httpsPost(String url, String param, Map<String, String> headMap) {
        JSONObject jsonObject = HttpHelper.httpsPost(url, param, headMap);
        return jsonObject;
    }

    @SneakyThrows
    private JSONObject httpGet(String url, Map<String, String> headMap) {
        JSONObject jsonObject = HttpHelper.httpGet(url, headMap);
        return jsonObject;
    }


}
